﻿using Abp;
using Abp.AspNetCore;
using Abp.AspNetCore.Configuration;
using Abp.AutoMapper;
using Abp.Configuration.Startup;
using Abp.Dependency;
using Abp.EntityHistory;
using Abp.Modules;
using Abp.Reflection.Extensions;
using Abp.Threading.Timers;
using Abp.Timing;
using KelonProud.Administration.Configuration;
using KelonProud.Domain.Entitys;
using KelonProud.Timing;

namespace KelonProud.Administration
{
    [DependsOn(typeof(KelonProudModule),
        typeof(AbpKernelModule),
        typeof(AbpAspNetCoreModule),
        typeof(AbpAutoMapperModule))]
    public class KelonProudAdministrationModule : AbpModule
    {
        public override void PreInitialize()
        {
            //// Declare entity types
         

            //// Enable this line to create a multi-tenant application.
            Configuration.MultiTenancy.IsEnabled = KelonProudConsts.MultiTenancyEnabled;
            Configuration.EntityHistory.IsEnabled = true;

            //// Configure roles

            Configuration.Settings.Providers.Add<AppSettingProvider>();
            //Configuration.Authorization.Providers.Add<HonkSoftAuthorizationProvider>();

            Configuration.Modules.AbpAspNetCore()
                .CreateControllersForAppServices(
                    typeof(KelonProudAdministrationModule).GetAssembly(), "admin", false
                );

            //实体历史
            Configuration.EntityHistory.Selectors.Add(new NamedTypeSelector("Kelon.AuditEntity", 
                type => typeof(IAuditEntity).IsAssignableFrom(type)));


            Configuration.ReplaceService<IEntityHistoryStore, EntityHistoryStore>(DependencyLifeStyle.Singleton);
        }

        public override void Initialize()
        {
            var thisAssembly = typeof(KelonProudAdministrationModule).GetAssembly();

            IocManager.RegisterAssemblyByConvention(thisAssembly);

            Configuration.Modules.AbpAutoMapper().Configurators.Add(
                // Scan the assembly for classes which inherit from AutoMapper.Profile
                cfg => cfg.AddProfiles(thisAssembly)
            );

            //Configuration.EmbeddedResources.Sources.Add(
            //    new EmbeddedResourceSet(
            //        "/Views/",
            //        thisAssembly,
            //        "HonkSoft.Administration.Views"
            //    )
            //);
        }

        public override void PostInitialize()
        {
            IocManager.Resolve<AppTimes>().StartupTime = Clock.Now;
        }
    }
}
